package com.jinsi.controller.zhu;



import com.jinsi.pojo.SyUser;
import com.jinsi.service.zhu.AccountService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.Past;
import java.util.Date;

/**
 * 获取用户信息，修改密码
 * @author 朱浩榕
 * @Date 2022-10-13
 */
@RestController
@RequestMapping("/home")
@Slf4j
@Validated
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 获取当前登陆用户信息
     * @param session 用户登陆后的session
     * @return
     */
    @RequestMapping("/getUser")
    public void updateisDel(HttpSession session) {
        System.out.println(session);
//        List<SyUser> syUsers = new ArrayList<>();
//        SyUser loginuser = (SyUser) session.getAttribute("loginuser");
//        SyUser user = accountService.getUser(loginuser.getUserId());
//        syUsers.add(user);
//        Result json = new Result(0,"成功",syUsers,0);
//        return json;
    }

    /**
     * 修改用户密码(不使用的MD5加密时的修改密码)
     * @param userPwd 用户的密码
     * @param userPwned 验证密码
     * @param userPawned 要修改的新密码
     * @param session 用户session
     * @return
     */
    @RequestMapping("/updatePwd")
    public R updatePwd(SyUser u,String userRemark,String userEmail,String userDateBirth, String userPwd, String userPwned, String userPawned, HttpSession session,String userpassword,@Min(1) int id) {
        R jsonData = null;
        System.out.println(u);

        if (!userpassword.equals(userPwd)){
            jsonData = R.ok("原始密码不正确");
            return jsonData;
        }
        if (!userPwned.equals(userPawned)){
            jsonData = R.ok("两次密码不一致");
            return jsonData;
        }
        Date date = new Date();
        log.info("修改用户密码：[开始],修改用户密码:{}");
        accountService.updatePwd(userPwned,date,id,userPawned,userEmail,userDateBirth);
        log.info("修改用户mim: [结束] 结果：{}","成功");
        jsonData = R.ok("成功");
        return jsonData;

    }

    /**
     * 修改思云用户个人信息
     * @param userId
     * @param userRemark
     * @param userEmail
     * @param userDateBirth
     * @return
     */
    @RequestMapping("/updateUserData")
    public R updataUserData(int userId, String userRemark, @Email String userEmail,@Past Date userDateBirth) {
        R jsonData = null;
        log.info("修改用户个人信息：[开始],修改个人信息:{}");
        accountService.updataUserData(userId,userRemark,userEmail,userDateBirth);
        log.info("修改用户个人信息: [结束] 结果：{}","成功");
        jsonData = R.ok("成功");
        return jsonData;
    }
}
